package cn.gailvlun.gll.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;

import cn.gailvlun.gll.event.AuthEvent;
import cn.gailvlun.gll.net.GsonUtil;
import cn.gailvlun.gll.presentation.apartment.ApartmentActivity;
import cn.gailvlun.gll.presentation.apartment.ApartmentDetailActivity;
import cn.gailvlun.gll.presentation.apartment.ApartmentManageActivity;
import cn.gailvlun.gll.presentation.chat.DialogListActivity;
import cn.gailvlun.gll.presentation.chat.FriendNotificationActivity;
import cn.gailvlun.gll.presentation.emotion.EmotionDetail2Activity;
import cn.gailvlun.gll.presentation.emotion.bean.EmotionDetailTransfer;
import cn.gailvlun.gll.util.JwtUtil;
import cn.gailvlun.gll.util.LogUtil;
import cn.jpush.android.api.JPushInterface;



public class JPushReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        LogUtil.d("onReceive - " + intent.getAction() + ", extras: " + bundle.toString());

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            LogUtil.d("JPush用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            LogUtil.d("接受到推送下来的自定义消息");
            processCustomMsg(context, bundle);
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            LogUtil.d("接受到推送下来的通知");

            receivingNotification(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            LogUtil.d("用户点击打开了通知");

            openNotification(context, bundle);

        } else {
            LogUtil.d("Unhandled intent - " + intent.getAction());
        }
    }

    private void processCustomMsg(Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);
        LogUtil.d(" title : " + title);
        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        LogUtil.d("message : " + message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        LogUtil.d("extras : " + extras);

        JPushPayload jPushPayload = GsonUtil.getGson().fromJson(extras, JPushPayload.class);
        switch (jPushPayload.getType()) {
            case JPushPayload.changeDevice: {
                LogUtil.e("extras" + extras);
                EventBus.getDefault().post(new AuthEvent());
                break;
            }
        }
    }

    private void receivingNotification(Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        LogUtil.d(" title : " + title);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        LogUtil.d("message : " + message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        LogUtil.d("extras : " + extras);
    }

    private void openNotification(Context context, Bundle bundle) {
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        JPushBody jPushBody = GsonUtil.getGson().fromJson(extras, JPushBody.class);

        JPushPayload jPushPayload = JwtUtil.isPayloadTrustable(jPushBody.getPayload(), JPushPayload.class);
        if (jPushPayload == null) {
            Log.d("test", "服务器端出错");
            return;
        }

        switch (jPushPayload.getType()) {
            case JPushPayload.heartEmotion:
            case JPushPayload.commentEmotion: {
                EmotionDetailTransfer transfer = new EmotionDetailTransfer();
                transfer.setId(jPushPayload.getId());
                EmotionDetail2Activity.openFromNotification(context, transfer);
                break;
            }
            case JPushPayload.inviteFriend: {
                FriendNotificationActivity.openFromNotification(context);
                break;
            }
            case JPushPayload.ensureInviteFriend: {
                DialogListActivity.openFromNotification(context);
                break;
            }
            case JPushPayload.chat: {
                DialogListActivity.openFromNotification(context);








                break;
            }
            case JPushPayload.createRoom: {
                DialogListActivity.openFromNotification(context);
                break;
            }
            case JPushPayload.preCloseRoom: {
                DialogListActivity.openFromNotification(context);
                break;
            }
            case JPushPayload.closeRoom: {
                ApartmentManageActivity.openFromNotification(context);
                break;
            }
            case JPushPayload.roomTask: {
                DialogListActivity.openFromNotification(context);
                break;
            }
            case JPushPayload.roomReviewResult: {

                ApartmentDetailActivity.openFromNotification(context,jPushPayload.getId());
                break;
            }
            case JPushPayload.startRegisterRoom: {
                ApartmentActivity.openFromNotification(context);
                break;
            }
        }
    }
}
